package com.zlcx.tz.live.web;

import com.zlcx.tz.live.utils.SimpleLockUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author zl
 * @version 1.0
 * @date Created in 2020/6/2
 */
@Controller
@Slf4j
@Api(tags = {"task"})
@RequestMapping("task")
public class TaskController {

    @Autowired
    private SimpleLockUtil simpleLockUtil;


    @ApiOperation(value = "关闭定时任务")
    @GetMapping(value = "/close")
    public Boolean close(@ApiParam(value = "token") @RequestHeader(required = false) String authorization,
                         @ApiParam(value = "lockKey", required = true) @RequestParam String lockKey,
                         @ApiParam(value = "taskClassName") @RequestParam(required = false, defaultValue = "com.zlcx.tz.live.service.TaskService") String taskClassName) {

        return simpleLockUtil.acquireLock(lockKey, this.getClass().getName(), 12 * 24 * 60 * 60 * 1000);
    }

    @ApiOperation(value = "开启定时任务")
    @GetMapping(value = "/open")
    public Boolean open(@ApiParam(value = "token") @RequestHeader(required = false) String authorization,
                        @ApiParam(value = "lockKey", required = true) @RequestParam String lockKey,
                        @ApiParam(value = "taskClassName") @RequestParam(required = false, defaultValue = "com.zlcx.tz.live.service.TaskService") String taskClassName) {

        return simpleLockUtil.releaseLock(lockKey, taskClassName);
    }


}
